/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.logical.allhit.store;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InvalidClassException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

/**
 * Basie class to manage storing of all setting and object
 * @author Sanjeet Jha
 */
public class StorageManager {
    
    private static final String STORE_DIR_NAME = "AllHit";

    /**
     * name of cache dir
     */
    private static final String CACHE_DIR = "cache";
    /**
     * Name of setting dir
     */
    private static final String SETTINGS_DIR = "settings";
    private static final StorageManager instance = new StorageManager();
    /**
     * Application's home dir
     */
    private final File storeDirectory;

    public static StorageManager getInstance() throws IOException {
        return instance;
    }

    private StorageManager() {
        File homeDir = new File(System.getProperty("user.home"));
        File settingsDir = new File(homeDir, STORE_DIR_NAME);
        this.storeDirectory = settingsDir;
        if (!this.storeDirectory.exists()) {
            this.storeDirectory.mkdirs();
        }
    }
    
    public File getAppHome() {
    	return this.storeDirectory;
    }

    public File getCacheRoot() {
        return new File(this.storeDirectory, CACHE_DIR);
    }

    public File getSettingsDirectory() {
    	return new File(this.storeDirectory, SETTINGS_DIR);
    }

    public void saveSettings(String name, Serializable data) throws IOException {
    	File dir = this.getSettingsDirectory();
    	if(!dir.exists()) {
    		dir.mkdirs();
    	}
    	File file = new File(dir, name);
    	OutputStream out = new FileOutputStream(file);
    	try {
    		BufferedOutputStream bos = new BufferedOutputStream(out);
    		ObjectOutputStream oos = new ObjectOutputStream(bos);
    		oos.writeObject(data);
    		oos.flush();
    	}
        catch(Exception e){
            
        } finally {
    		out.close();
    	}
    }

    public Serializable retrieveSettings(String name, ClassLoader classLoader) throws IOException, ClassNotFoundException {
    	File dir = this.getSettingsDirectory();
    	if(!dir.exists()) {
    		return null;
    	}
    	File file = new File(dir, name);
    	if(!file.exists()) {
    		return null;
    	}
    	InputStream in = new FileInputStream(file);
    	try {
    		BufferedInputStream bin = new BufferedInputStream(in);
    		ObjectInputStream ois = new ClassLoaderObjectInputStream(bin, classLoader);
    		try {
    			return (Serializable) ois.readObject();
    		} catch(InvalidClassException ice) {
    			ice.printStackTrace();
    			return null;
    		}
    	} finally {
    		in.close();
    	}
    }
}
